package com.yiwei.zhh.activity;

import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.zhh.R;
import com.yiwei.zhh.api.callback.ForgetPwdContract;
import com.yiwei.zhh.api.requestInfo.ForgetPwdRequest;
import com.yiwei.zhh.util.PreferenceUtils;
import com.yiwei.zhh.constant.Const;
import com.yiwei.zhh.model.ResponseModel;
import com.yiwei.zhh.util.ImageUtil;
import com.yiwei.zhh.util.MD5;
import com.yiwei.zhh.util.ToastUtil;
import com.yiwei.zhh.view.CircularProgress;

public class ForgetPwdActivity extends BaseActivity implements OnClickListener, ForgetPwdContract.View {

    private TextView codeBtn;
    private EditText codeEt;
    private boolean isSend;
    private CircularProgress loading;
    //    private ImageView logo;
    private CheckBox mSeeBtn;
    private EditText newPwd;
    private LinearLayout oneView;
    private EditText phoneEt;
    private TextView btnOne, btnTwo;

    private ForgetPwdRequest api;

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setBaseContentView(R.layout.forget_pwd_activity);
        baseInitView(this, "ForgetPwdActivity", Const.ViewType.ACTIVITY);
        initView();
    }


    private CountDownTimer timer = new CountDownTimer(60000L, 1000L) {
        public void onFinish() {
            ForgetPwdActivity.this.resetBtn();
        }

        @TargetApi(16)
        public void onTick(long paramAnonymousLong) {
            ForgetPwdActivity.this.codeBtn.setEnabled(false);
            ForgetPwdActivity.this.codeBtn.setText(paramAnonymousLong / 1000L + ForgetPwdActivity.this.getResources().getString(R.string.resend));
            ForgetPwdActivity.this.codeBtn.setTextColor(ContextCompat.getColor(ForgetPwdActivity.this.getApplicationContext(), R.color.gray));
        }
    };
    private LinearLayout twoView;
    private int type;

    private boolean checkCode() {
        if (TextUtils.isEmpty(this.codeEt.getText().toString())) {
            ToastUtil.makeText(this, getResources().getString(R.string.code_is_empty), 0).show();
            return false;
        }
        return true;
    }

    private boolean checkNewPwd() {
        if (TextUtils.isEmpty(this.newPwd.getText().toString())) {
            ToastUtil.makeText(this, getResources().getString(R.string.password_is_empty), 0).show();
            return false;
        }
        return true;
    }

    private boolean checkPhone() {
        if (TextUtils.isEmpty(this.phoneEt.getText().toString())) {
            ToastUtil.makeText(this, getResources().getString(R.string.phone_is_empty), 0).show();
            return false;
        }
        if (this.phoneEt.getText().toString().length() < 11) {
            ToastUtil.makeText(this, getResources().getString(R.string.phone_is_error), 0).show();
            return false;
        }
        return true;
    }

    private void getCheckCode() {
        api.getCheckCodeInfo(phoneEt.getText().toString().trim(), codeEt.getText().toString().trim());
    }

    private void getCode() {
        api.getCodeInfo("sms", phoneEt.getText().toString().trim());
    }

    private void getResetPwd() {
        api.getResetPwdInfo(this.phoneEt.getText().toString().trim(), MD5.getMD5str(this.newPwd.getText().toString().trim()), this.codeEt.getText().toString().trim());
    }

    private void getUpdatePwd() {
        api.getUpdatePwdInfo(MD5.getMD5str(this.newPwd.getText().toString().trim()));
    }

    private void initView() {
        api = ForgetPwdRequest.create();
        api.attachView(this);

        boolean isUpdate = getIntent().getBooleanExtra("update", false);
        if (isUpdate) {
            setTitleText("修改密码");
        } else {
            setTitleText(getResources().getString(R.string.forget_pwd));
        }

        setBackIntent();
        this.mSeeBtn = ((CheckBox) findViewById(R.id.see));
        this.loading = ((CircularProgress) findViewById(R.id.progressBar));
        this.oneView = ((LinearLayout) findViewById(R.id.forget_one));
        this.twoView = ((LinearLayout) findViewById(R.id.forget_two));
        this.phoneEt = ((EditText) findViewById(R.id.phone));
        this.codeEt = ((EditText) findViewById(R.id.code));
        this.newPwd = ((EditText) findViewById(R.id.new_pwd));
        this.codeBtn = ((TextView) findViewById(R.id.code_tv));
        this.codeBtn.setEnabled(true);
        this.codeBtn.setOnClickListener(this);
        btnOne = (TextView) findViewById(R.id.next_one);
        btnTwo = (TextView) findViewById(R.id.next_two);
        btnOne.setOnClickListener(this);
        btnTwo.setOnClickListener(this);

        this.mSeeBtn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton paramAnonymousCompoundButton, boolean paramAnonymousBoolean) {
                if (paramAnonymousBoolean) {
                    ForgetPwdActivity.this.mSeeBtn.setBackgroundResource(R.mipmap.see_off);
                    ForgetPwdActivity.this.newPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    ForgetPwdActivity.this.mSeeBtn.setBackgroundResource(R.mipmap.see);
                    ForgetPwdActivity.this.newPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                newPwd.setSelection(newPwd.getText().length());
            }
        });

        boolean show = getIntent().getBooleanExtra("showLogo", false);
        this.type = getIntent().getIntExtra("type", 0);
        if (this.type == 1) {
            this.oneView.setVisibility(View.VISIBLE);
            this.twoView.setVisibility(View.GONE);
        } else {
            this.oneView.setVisibility(View.GONE);
            this.twoView.setVisibility(View.VISIBLE);
        }

        if ((PreferenceUtils.getBoolean(this, "is_logined", false)) && (!TextUtils.isEmpty(PreferenceUtils.getString(this, "mobile", "")))) {
            this.phoneEt.setText(PreferenceUtils.getString(this, "mobile", ""));
            this.phoneEt.setEnabled(false);
        }

    }

    @TargetApi(16)
    private void resetBtn() {
        if (this.timer != null)
            this.timer.cancel();

        if (this.isSend)
            this.codeBtn.setText(getResources().getString(R.string.send_code_again));
        else this.codeBtn.setText(getResources().getString(R.string.get_code));

        this.codeBtn.setEnabled(true);
        this.codeBtn.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.blue));
    }


    public void onClick(View paramView) {
        switch (paramView.getId()) {
            case R.id.code_tv:
                if (checkPhone()) {
                    this.timer.start();
                    getCode();
                }
                break;
            case R.id.next_one:
                if (checkPhone() && checkCode())
                    getCheckCode();
                break;
            case R.id.next_two:
                if (checkNewPwd())
                    if (this.type == 1) {
                        getResetPwd();
                    } else getUpdatePwd();
                break;
        }
    }


    /**
     * 是否显示加载框
     *
     * @param b
     */
    private void showLoading(boolean b) {
        if (loading != null) {
            if (b) {
                btnOne.setEnabled(false);
                btnTwo.setEnabled(false);
                loading.setVisibility(View.VISIBLE);
            } else {
                btnOne.setEnabled(true);
                btnTwo.setEnabled(true);
                loading.setVisibility(View.GONE);
            }
        }
    }


    protected void onDestroy() {
        super.onDestroy();
        if(timer!=null){
            timer.cancel();
            timer=null;
        }
        api.detachView();
//        ImageUtil.releaseImageViewResouce(this.logo);
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {
        showLoading(false);
    }

    @Override
    public void onRequestStart() {
        showLoading(true);
    }

    @Override
    public void getCheckCodeInfoSuccess(ResponseModel item) {
        ForgetPwdActivity.this.oneView.setVisibility(View.GONE);
        ForgetPwdActivity.this.twoView.setVisibility(View.VISIBLE);
    }

    @Override
    public void getCodeInfoSuccess(ResponseModel model) {
        ToastUtil.makeText(ForgetPwdActivity.this.getApplicationContext(), model.getMsg(), 0).show();
    }

    @Override
    public void getResetPwdInfoSuccess(ResponseModel model) {
        ToastUtil.makeText(ForgetPwdActivity.this.getApplicationContext(), model.getMsg(), 0).show();
        ForgetPwdActivity.this.finish();
    }

    @Override
    public void getUpdatePwdInfoSuccess(ResponseModel model) {
        ToastUtil.makeText(ForgetPwdActivity.this.getApplicationContext(), model.getMsg(), 0).show();
        ForgetPwdActivity.this.finish();
    }
}
